#include<vector>
#include<algorithm>
using namespace std;

class Solution {
public:
    void nextPermutation(vector<int>& nums) {
        int n = nums.size();
        int i = n - 1, j = n - 1;
        while (j >= 0 && nums[i] <= nums[j]) {
            j --;
        }

        if (j < 0) {
            sort(nums.begin(), nums.end());
        } else{
            swap(nums[i], nums[j]);
        }
    }
};